<?xml version="1.0" encoding="windows-1255" ?>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="cs236369.hw5.DBConnection"%>
<%@ page import="cs236369.hw5.InitListener" %>
<%@ page import="cs236369.hw5.Mission" %>
<%@ page import="cs236369.hw5.Skill" %>
<%@ page import="cs236369.hw5.Team" %>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
	<%	if(session == null || session.getAttribute("userName") == null || 
				request.getParameter("postId") == null) {
			response.setStatus(404);
			response.sendRedirect(request.getContextPath() + InitListener.welcomePage);
			return;
		}
		Mission mission = DBConnection.getMission(Integer.parseInt(request.getParameter("postId")));
		if(mission == null) {
			response.sendError(400, "Invalid task requested");
			return;
		}
		Boolean taskOwner = ((String)session.getAttribute("userName")).equals(mission.getOwner()) ? true : false;
		String disabled = taskOwner ? "" : "disabled";
	%>
	<title>Task <%= mission.getName() %></title>
	<!-- initialize google maps scripts and what to load -->
	<script src="googleMapScripts.js" type="text/javascript"></script>
	<script> 
		googleMapScripts_loadTasks=false;		
		googleMapScripts_loadTeams=false;		
		googleMapScripts_loadMatches=false; 
		googleMapScripts_loadUserPosts=false;
		googleMapScripts_loadSinglePostType=0;
		googleMapScripts_loadSinglePostWithID=<%= mission.getId() %>;
	</script>
</head>
<body class="mapPageBody">
	<p class="subtitle">Task &nbsp; <%= mission.getName() %><br /></p>
	<p class="topic">Position:</p>
	<div id="map-canvas" class="map-canvas"></div>
	
	<div class="text"><form action="#" method="post" id="editPost">
		<p class="topic">Task:&nbsp;<%= mission.getName() %></p>
		<label>Number of people to evacuate:&nbsp;</label>
		<input type="text" name="capacity" id="capacity" value=<% out.print("\"" + mission.getCapacity() + "\" " + disabled); %> /><br />
		<label>Description:&nbsp;</label>
		<input type="text" name="content" id="content" value=<% out.print("\"" + mission.getContent() + "\" " + disabled); %> /><br />
		<label>task skills:</label> <br />
		<%
			HashMap<Integer, Integer> idToIndex = new HashMap<Integer, Integer>();
			if(taskOwner) {
				out.println("<select id=\"skill1\" onchange=\"addSkillBox(this)\">");
				out.println("<option value=\"\" disabled=\"disabled\" selected=\"selected\">Add skill</option>");
				ArrayList<Skill> skills = DBConnection.getSkills(true);
				int optIndex = 1, skillId;
				for(Skill skill : skills) {
					skillId = skill.getId();
					out.print("<option id=\"opt" + skillId + "\" value=\"" + skillId + "\"");
					if(mission.doesHaveSkill(skillId)) {
						out.print(" disabled");
						idToIndex.put(skillId, optIndex);
					}
					out.println(" >" + skill.getSkill() + "</option>");
					optIndex++;
				}
				out.print("</select>");
			}
		%>
		<div id="skillsListDiv" class="text">
		<%
			Skill[] taskSkills = mission.getSkills();
			int index, skillId;
			for(int i = 0; i < taskSkills.length;  i++) {
				skillId = taskSkills[i].getId();
				index = idToIndex.containsKey(skillId) ? idToIndex.get(skillId) : i + taskSkills.length;
				out.println("<p id=\"newSkill" + index + "\">\n" + taskSkills[i].getSkill());
				if(taskOwner) {
					out.println("<img src=\"images/delete.png\" onclick=\"removeSelectedSkill(" + index + ")\">");
					out.println("<input value=\"" + skillId + "\" name=\"newSkillId1\" type=\"hidden\">");
				}
				out.println("</p>");
			}
		%>
		</div>
		<p class="topic">Teams joined to task:</p><ol>
		<%	for(Team team : mission.getTeamsJoined()) {
				out.print("<li><label onclick=\"loadToMainContent(\'team.jsp?postId=" + team.getId() + "\')\">id " +
						team.getId() + ": " + team.getName() + "</label></li><br />");
			}	%>
		</ol><br />
		<input type="hidden" name="taskId" id ="taskId" value="<%= mission.getId() %>" />
		<%
			if(taskOwner) {
				out.print("<input type=\"button\" class=\"regularButton\" value=\"Update\" onclick=\"updatePost(\'UpdateTask\', \'editPost\')\"/>");
			}
		%>
	</form>
	<form id="matching">
		<input type="hidden" name="taskId" value="<%= mission.getId() %>" />
		<input type="hidden" name="command" value="match task" />
		<%
		Integer matchingTeamId = (Integer) session.getAttribute("matchingTeamId");
			String matchingTeamName = (String) session.getAttribute("matchingTeamName");
			String buttonText;
			if(matchingTeamId == null || matchingTeamName == null) {
				buttonText = "Recruit a team to this task";
			} else {
				buttonText = "Recruit team " + matchingTeamName + " to this task";
			}
		%>
		<input type="button" value="<%=buttonText %>" onclick="updatePost('UpdateTask', 'matching')" />
	</form>
	<form id="taskManagment">
		<input type="hidden" name="taskId" id ="taskId" value="<%= mission.getId() %>" />
		<%
			if(taskOwner) {
				out.print("<input type=\"button\" class=\"regularButton\" value=\"Delete task\" onclick=\"updatePost(\'UpdateTask\', \'taskManagment\')\"/>");
				out.println("<input value=\"delete\" name=\"command\" type=\"hidden\">");
			}
		%>
	</form></div><br />
</body>
</html>
